#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
线性单元
'''
from perceptron import Perception

# 定义激活函数
activator = lambda x:x

class LinearUnit(Perception):
    '''
    通过继承感知器，实现线性单元
    '''
    def __init__(self, input_num):
        '''
        初始化线性单元，设置输入的参数个数
        :param input_num:
        '''
        Perception.__init__(self, input_num, activator)

def generate_train_dataset():
    # 构建训练数组
    input_vecs = [[5],[3],[8],[1.4],[10.1]]
    # 期望的输出列表
    labels = [5500, 2300, 7600, 1800, 11400]
    return input_vecs,labels

def train_linear_unit():
    '''
    训练线程单元
    :return:
    '''
    # 创建感知器，输入参数的特征数为1
    linear_perceptron = LinearUnit(1)
    # 开始训练
    input_vecs,labels = generate_train_dataset()
    linear_perceptron.train(input_vecs,labels, 10, 0.01)
    return linear_perceptron

if __name__ == '__main__':
    '''训练线性单元'''
    lu = train_linear_unit()
    # 打印训练获得的权重
    print lu
    # 测试
    print 'Work 3.4 years, monthly salary = %.2f' % lu.predict([3.4])
    print 'Work 15 years, monthly salary = %.2f' % lu.predict([15])
    print 'Work 1.5 years, monthly salary = %.2f' % lu.predict([1.5])
    print 'Work 6.3 years, monthly salary = %.2f' % lu.predict([6.3])
